//
//  DZAgioPreferentialViewController.m
//  DaZhePrivateHomeCuisineClient
//
//  Created by zouzhisheng on 14-12-9.
//  Copyright (c) 2014年 zouzhisheng. All rights reserved.
//

#import "DZAgioPreferentialViewController.h"
#import "DZWebViewController.h"
#import "DZDinningDetailViewController.h"
@implementation DZAgioPreferentialNav
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   // self.view.backgroundColor = [UIColor clearColor];
    DZAgioPreferentialViewController *classVC=[[DZAgioPreferentialViewController alloc] init];
    self.viewControllers = @[classVC];
    
}


@end


@interface DZAgioPreferentialViewController (){
     NSMutableArray *dataArray;
}

@end

@implementation DZAgioPreferentialViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    dataArray=[NSMutableArray array];
    [self addTitle:@"折扣优惠"];
    self.navigationController.navigationBarHidden=YES;
    [self navView];
    [self loadData];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [SharedAppDelegate.home.tabBarController setTabBarHidden:NO];
}
-(void)loadData{
    NSDictionary *paramDir=[NSDictionary dictionaryWithObjectsAndKeys:@"zhekou",@"act", nil];
    [[BaseService shareNetworkService] requestActivityParameters:paramDir DataSouce:self RequestType:Request_DaZheTuiJianZhekou Block:^(__weak id data, NSError *error) {
        if (!error) {
            DZAdviceInfoSubModel *adviceInfoSubModel=SharedAppDelegate.home.adviceInfoModel.adviceInfo[1];
            [dataArray addObject:adviceInfoSubModel];
            DZSubjectZhekouInfoModel *model=data;
            if (model.subjectZhekouInfo.count>0) {
                for (int i=0; i<model.subjectZhekouInfo.count; i++) {
                    [dataArray addObject:model.subjectZhekouInfo[i]];
                }
            }
            [self loadSubView];
    
        }
    }];
    
}
-(void)loadSubView
{
    [self.view addSubview:self.listView];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)backAction:(UIButton*)bt
{
    [SharedAppDelegate.home.tabBarController.navigationController popViewControllerAnimated:YES];
}
-(UITableView *)listView
{
    if (!_listView) {
        _listView=[[UITableView alloc] initWithFrame:CGRectMake(0,self.navView.bottom,SCREEN_WIDTH,SYSTEM_VERSION>=7.0?SCREEN_HEIGHT-self.navView.height-49:SCREEN_HEIGHT-self.navView.height-20-49) style:UITableViewStylePlain];
        _listView.delegate=self;
        _listView.dataSource=self;
        _listView.backgroundColor=[UIColor clearColor];
        _listView.separatorStyle=UITableViewCellSeparatorStyleNone;
    }
    return _listView;
}
#pragma mark-UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id obj=dataArray[indexPath.row];
    if ([obj isKindOfClass:[DZSubjectZhekouInfoSubModel class]]) {
        static NSString *cellIdentifier=@"DZYHTableViewCell";
        DZYHTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"DZYHTableViewCell"  owner:self options:nil] lastObject];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell loadData:obj];
        return cell;
    }
    if ([obj isKindOfClass:[DZAdviceInfoSubModel class]]) {
        DZAdviceInfoSubModel *model=obj;
        static NSString *cellIdentifier=@"DZAdviceTableViewCell";
        DZAdviceTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"DZAdviceTableViewCell"  owner:self options:nil] lastObject];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        [cell loadImageUrlStr:model.pic];
        return cell;
    }
    
    return nil;
}

#pragma mark-UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id obj=dataArray[indexPath.row];
    if ([obj isKindOfClass:[DZSubjectZhekouInfoSubModel class]]) {
        return 90;
    }
    if ([obj isKindOfClass:[DZAdviceInfoSubModel class]]){
//        DZAdviceInfoSubModel *model=obj;
//        CGSize size=[Common downloadImageSizeWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",API_DOMAIN,model.pic]]];
//        float imageH=size.height*SCREEN_WIDTH/size.width;
        return 49;
    }
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    id obj=dataArray[indexPath.row];
    if ([obj isKindOfClass:[DZAdviceInfoSubModel class]]) {
        DZAdviceInfoSubModel *model=obj;
        DZWebViewController *webVC=[[DZWebViewController alloc] init];
        webVC.adviceInfoSubModel=model;
        [self.navigationController pushViewController:webVC animated:YES];
    }else{
        DZSubjectZhekouInfoSubModel *subModel=obj;
        DZDinningDetailViewController *detailVC=[[DZDinningDetailViewController alloc] init];
        detailVC.isZhekou=YES;
        detailVC.sid=subModel.sid;
        detailVC.youhui=subModel.c_zhekouyouhui;
        detailVC.youhuineirong=subModel.c_zhekouneirong;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}
-(void)tableView:(UITableView*)tableView  willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
    
}
- (UIView *)viewForLoadAnimationAtService:(RequestType)type{
    return self.view;
}
-(void)reloadCellHeigh:(float)height{
    self.cellHeight=height;
    [self.listView beginUpdates];
    [self.listView endUpdates];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
